package wxapi;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.alstudio.module.sns.weiboshare.IWeiboShare.Callback;
import com.alstudio.view.toast.MyToastUtil;
import com.loovee.imaohu.R;
import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXWebpageObject;

/**
 * 微信分享
 * 
 * @author jinx
 * 
 */
public class WeiXin implements IWXAPIEventHandler {
	private IWXAPI api;
	private Context mContext;
	public static final int THUMB_SIZE = 150;
	public static Callback callback;

	public WeiXin(Context context, String appId) {
		this.mContext = context;
		// 通过WXAPIFactory工厂，获取IWXAPI的实例
		api = WXAPIFactory.createWXAPI(mContext, appId, false);
		// 将该app注册到微信
		api.registerApp(appId);
	}

	/**
	 * 分享到微信
	 * 
	 * @param imageFile
	 *            本地图片地址 图片地址
	 * @param dstWidth
	 * @param dstHeight
	 * @param flag
	 * 
	 */
	public void sharePersonaHomeWeiXin(String content, String imageUrl,
			int dstWidth, int dstHeight) {
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = getShareUrl(imageUrl);

		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title = content;
		msg.description = mContext.getString(
				R.string.TxtWeixinShareContent);

		Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.weixin_share_icon);
		msg.thumbData = Util.bmpToByteArray(thumb, true);

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("webpage");
		req.message = msg;
		req.scene = SendMessageToWX.Req.WXSceneSession;
		api.sendReq(req);
	}

	/**
	 * 分享到朋友圈
	 * 
	 * @param imageFile
	 *            图片Url地址
	 * @param dstWidth
	 * @param dstHeight
	 * @param flag
	 */

	public void sharePersonaHomeWeiXinFri(String content, String imageUrl,
			int dstWidth, int dstHeight) {

		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = getShareUrl(imageUrl);

		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title = content;
		msg.description = mContext.getString(
				R.string.TxtWeixinShareContent);

		Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.weixin_share_icon);
		msg.thumbData = Util.bmpToByteArray(thumb, true);

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("webpage");
		req.message = msg;
		req.scene = SendMessageToWX.Req.WXSceneTimeline;
		api.sendReq(req);
	}

	public static String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis())
				: type + System.currentTimeMillis();
	}

	private static String getShareUrl(String imageUrl) {
		String url = "http://www.imaohu.com";
		if (!TextUtils.isEmpty(imageUrl)) {
			if (imageUrl.startsWith("http")) {
				url = imageUrl;
			}
		}
		return url;
	}

	@Override
	public void onReq(BaseReq req) {

	}

	@Override
	public void onResp(BaseResp resp) {
		int result = 0;

		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			result = R.string.TxtSnsSharesuccess;
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = R.string.TxtSnsShareFail;
			break;
		}
		MyToastUtil.getInstance().showCenterToastOnCenter(
				mContext.getString(result));
	}
}
